import os

import xlrd  # xlrd=1.2.0,不然不能读取xlsx文件，只能读取xls，或者用其他代替

base_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
read_xlrd = base_path + os.sep + 'excel_data'


class Common:
    """读取Excel方法"""
    def ReadExcelTypeDict(self, file_name, path=read_xlrd):
        path = path + '/' + file_name
        work_book = xlrd.open_workbook(path)  # 打开excel表
        sheets = work_book.sheet_names()  # 获取所有sheets页
        new_list = []
        for sheet in sheets:
            sheet_ob = work_book.sheet_by_name(sheet)  # 返回对应sheets名称的sheet对象
            nrows = sheet_ob.nrows  # 获取sheet表行数
            data_list = []
            if nrows == 0:
                continue
            for i in range(0, nrows):
                values = sheet_ob.row_values(i)  # 获取xlsx每行的数据
                data_list.append(values)
            titleList = data_list[0]
            contentList = data_list[1:]
            for content in contentList:
                dic = {}
                for i in range(len(content)):
                    dic[titleList[i]] = content[i]
                new_list.append(dic)
        return new_list


if __name__ == "__main__":
    r = Common()
    print(r.ReadExcelTypeDict('superviseCase.xlsx'))
